package com.hkjtgc.pcr_app.ui.login

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import android.widget.Button
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatEditText
import com.hkjtgc.pcr_app.R
import com.hkjtgc.pcr_app.entity.bean.DataBean
import com.hkjtgc.pcr_app.entity.bean.GetLoginBean
import com.hkjtgc.pcr_app.entity.bean.Institution
import com.hkjtgc.pcr_app.entity.bean.PostLoginBean
import com.hkjtgc.pcr_app.service.LoginService
import com.hkjtgc.pcr_app.ui.MainActivity
import com.hkjtgc.pcr_app.util.*
import com.hkjtgc.pcr_app.util.Constant
import com.hkjtgc.pcr_app.util.ToastUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch


@RequiresApi(Build.VERSION_CODES.M)
class LoginActivity : AppCompatActivity() {

    lateinit var account: AppCompatEditText
    lateinit var password: AppCompatEditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        //用于判断是否为第一次登录(登录成功一次后，此处即不为空)
        if (SharedPreferencesUtils.getSPInstance(this).getSP(Constant.ACCOUNT) != "") {
            startActivity(Intent(this, MainActivity::class.java))
            return
        }
        setContentView(R.layout.activity_login)
        init()
    }


    private fun init() {
        account = findViewById(R.id.ed_account)
        password = findViewById(R.id.ed_password)

        findViewById<Button>(R.id.bt_login).setOnClickListener {
            val account = account.text.toString()
            if (account == "") {
                ToastUtil.toastShort(this, "请输入账号!")
                return@setOnClickListener
            }

            val password = password.text.toString()
            if (password == "") {
                ToastUtil.toastShort(this, "请输入密码!")
                return@setOnClickListener
            }

            when (NetWorkUtils.isConnected(this)) {
                true -> login(PostLoginBean(account, password))
                else -> ToastUtil.toastShort(this, "网络不可用,请稍后重试!")
            }
        }
    }

    private fun login1(postLoginBean: PostLoginBean) {
        val service: LoginService = RetrofitUtil.instance.retrofit.create(LoginService::class.java)
        GlobalScope.launch(Dispatchers.Main) {
            val response = service.login(postLoginBean.account, postLoginBean.password)
            if (response.ok) {
                ToastUtil.toastShort(this@LoginActivity, "登录成功!")
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.ACCOUNT, postLoginBean.account)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.PASSWORD, postLoginBean.password)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.TOKEN, response.msg)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.USER_NAME, response.data.name)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.ROLE_TYPE_NAME, response.data.roleName)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.INSTITUTION_NAME, response.data.institution.name)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.INSTITUTION_CONTACTS, response.data.institution.contacts)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.INSTITUTION_MOBILE, response.data.institution.mobile)
                SharedPreferencesUtils.getSPInstance(this@LoginActivity)
                    .putSP(Constant.INSTITUTION_ADDRESS, response.data.institution.address)
                startActivity(Intent(this@LoginActivity, MainActivity::class.java))
            }else {
                Log.e("cccc", response.code.toString())
            }
        }
    }


    private fun login(postLoginBean: PostLoginBean) {
        //todo 调用登录接口
        val response = GetLoginBean(0,"", DataBean(1,"admin","admin","管理员(出入库管理)","1","管理员", Institution(1,"StarArray","张三","15261852398","XXX路11号","MEDICAL","1")),true)

        when(response.code) {
            0 -> {
                ToastUtil.toastShort(this, "登录成功!")
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.ACCOUNT,postLoginBean.account)
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.PASSWORD,postLoginBean.password)
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.TOKEN,response.msg)
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.USER_NAME,response.data.name)
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.ROLE_TYPE_NAME,response.data.roleName)

                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.INSTITUTION_NAME,response.data.institution.name)
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.INSTITUTION_CONTACTS,response.data.institution.contacts)
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.INSTITUTION_MOBILE,response.data.institution.mobile)
                SharedPreferencesUtils.getSPInstance(this).putSP(Constant.INSTITUTION_ADDRESS,response.data.institution.address)

                startActivity(Intent(this, MainActivity::class.java))
            }
           else -> {}
        }
    }
}